package org.jeecg.modules.demo.kcmx.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.demo.kcmx.entity.JxcChuku;
import org.jeecg.modules.demo.kcmx.entity.JxcKcmx;
import org.jeecg.modules.demo.kcmx.entity.JxcYsk;
import org.jeecg.modules.demo.kcmx.service.IJxcChukuService;
import org.jeecg.modules.demo.kcmx.service.IJxcKcmxService;
import org.jeecg.modules.demo.kcmx.service.IJxcYskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;

 /**
 * @Description: 出库详情
 * @Author: jeecg-boot
 * @Date:   2024-07-02
 * @Version: V1.0
 */
@Api(tags="出库详情")
@RestController
@RequestMapping("/kcmx/jxcChuku")
@Slf4j
public class JxcChukuController extends JeecgController<JxcChuku, IJxcChukuService> {
	@Autowired
	private IJxcChukuService jxcChukuService;
	@Autowired
	private IJxcKcmxService jxcKcmxService;
	@Autowired
	private IJxcYskService jxcYskService;

	/**
	 * 分页列表查询
	 *
	 * @param jxcChuku
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "出库详情-分页列表查询")
	@ApiOperation(value="出库详情-分页列表查询", notes="出库详情-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<JxcChuku>> queryPageList(JxcChuku jxcChuku,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<JxcChuku> queryWrapper = QueryGenerator.initQueryWrapper(jxcChuku, req.getParameterMap());
		Page<JxcChuku> page = new Page<JxcChuku>(pageNo, pageSize);
		IPage<JxcChuku> pageList = jxcChukuService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
	 *   添加
	 *
	 * @param jxcChuku
	 * @return
	 */
	@AutoLog(value = "出库详情-添加")
	@ApiOperation(value="出库详情-添加", notes="出库详情-添加")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody JxcChuku jxcChuku) {
		Date date = new Date();
		SimpleDateFormat df1 = new SimpleDateFormat("yyyy-MM-dd HH：mm");
		SimpleDateFormat df2 = new SimpleDateFormat("yyyyMMddHHmmss");
		String id = "ZC"+df2.format(date);
		String zcOnly = jxcChuku.getZcOnly();
		String zcShiji = jxcChuku.getZcShiji();
		String ZcName = jxcChuku.getZcName();
		String zcOld = jxcChuku.getZcOld();
		jxcChuku.setId(id);
		System.out.println("zcOnly:"+zcOnly+"zcShiji:"+zcShiji);
		JxcKcmx jxcKcmx = jxcKcmxService.getByOnly(zcOnly,jxcChuku.getCompanyId());
		if (jxcKcmx == null) {
			JxcKcmx jxcKcmx1 = new JxcKcmx();
			jxcKcmx1.setId(id);
			jxcKcmx1.setZcOnly(zcOnly);
			jxcKcmx1.setZcName(ZcName);
			jxcKcmx1.setZcLittle(zcOnly);
			jxcKcmx1.setZcRuku("0");
			jxcKcmx1.setZcChuku(zcShiji);
			jxcKcmx1.setZcKucun("-"+zcShiji);
			jxcKcmx1.setZcOld(zcOld);
			jxcKcmx1.setZcSize("1");
			jxcKcmx1.setZcNew(zcOld);
			jxcKcmx1.setCompanyId(jxcChuku.getCompanyId());
			jxcKcmxService.save(jxcKcmx1);
//			return Result.error("未找到对应数据");
		}else {
			String zcKucun = jxcKcmx.getZcKucun();
			String zcChuku = jxcKcmx.getZcChuku();
//			String newKucun = String.valueOf(Double.parseDouble(zcKucun)-Double.parseDouble(zcShiji));
//			String newChuku = String.valueOf(Double.parseDouble(zcChuku)+Double.parseDouble(zcShiji));
			// 或者使用 BigDecimal 进行更精确的控制
			BigDecimal kucun = new BigDecimal(zcKucun);
			BigDecimal shiji = new BigDecimal(zcShiji);
			BigDecimal newKucunBD = kucun.subtract(shiji).setScale(0, RoundingMode.DOWN);
			String newKucun = newKucunBD.toString();

			BigDecimal chuku = new BigDecimal(zcChuku);
			BigDecimal newChukuBD = chuku.add(shiji).setScale(0, RoundingMode.DOWN);
			String newChuku = newChukuBD.toString();
			jxcKcmxService.setChuku(zcOnly, newChuku,newKucun,jxcChuku.getCompanyId());
		}
		String payno = "ZC"+df1.format(date);
		JxcYsk jxcYsk = new JxcYsk();
		String money = String.valueOf(Double.parseDouble(zcOld)*Double.parseDouble(zcShiji));
		jxcYsk.setId(id);
		jxcYsk.setZcPayno(payno);
		jxcYsk.setZcOnly(zcOnly);
		jxcYsk.setZcOldpay(money);
		jxcYsk.setZcPopay("");
		jxcYsk.setZcNewpay("");
		jxcYsk.setZcAccount("");
		jxcYsk.setZcLittle("");
		jxcYsk.setZcAction("");
		jxcYsk.setZcCompany("");
		jxcYsk.setCompanyId(jxcChuku.getCompanyId());
		jxcYskService.save(jxcYsk);

		jxcChukuService.save(jxcChuku);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param jxcChuku
	 * @return
	 */
	@AutoLog(value = "出库详情-编辑")
	@ApiOperation(value="出库详情-编辑", notes="出库详情-编辑")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody JxcChuku jxcChuku) {
//		String zcOnly = jxcChuku.getZcOnly();
//		String zcShiji = jxcChuku.getZcShiji();
//		System.out.println("zcOnly:"+zcOnly+"zcShiji:"+zcShiji);
//		try {
//			jxcKcmxService.setChuku(zcOnly, zcShiji);
//		}catch (Exception e){
//			System.out.println("--------------"+e);
//		}

		jxcChukuService.updateById(jxcChuku);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "出库详情-通过id删除")
	@ApiOperation(value="出库详情-通过id删除", notes="出库详情-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		jxcChukuService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "出库详情-批量删除")
	@ApiOperation(value="出库详情-批量删除", notes="出库详情-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.jxcChukuService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "出库详情-通过id查询")
	@ApiOperation(value="出库详情-通过id查询", notes="出库详情-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<JxcChuku> queryById(@RequestParam(name="id",required=true) String id) {
		JxcChuku jxcChuku = jxcChukuService.getById(id);
		if(jxcChuku==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(jxcChuku);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param jxcChuku
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, JxcChuku jxcChuku) {
        return super.exportXls(request, jxcChuku, JxcChuku.class, "出库详情");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, JxcChuku.class);
    }

}
